import { defineAsyncComponent } from 'vue'
import { Document, Menu as IconMenu, Location, Setting } from '@element-plus/icons-vue'

// 自动导入所有views下的路由组件
const modules = import.meta.glob('../views/**/index.vue')

// 路由配置与图标的映射
const routeConfig = {
  '/home': {
    title: '首页',
    icon: Location
  },
  '/about': {
    title: '关于',
    icon: Document
  },
  '/settings': {
    title: '设置',
    icon: Setting
  }
}

// 生成路由配置
const generateRoutes = () => {
  const routes = []

  for (const path in modules) {
    // 获取组件
    const component = modules[path]
    // 处理路由路径
    const routePath = path
      .replace('../views/', '/')
      .replace('/index.vue', '')
      .replace(/\/index$/, '')

    // 获取路由配置
    const config = routeConfig[routePath] || {
      title: routePath.split('/').pop(),
      icon: IconMenu
    }

    // 添加路由配置
    routes.push({
      path: routePath,
      name: routePath.slice(1),
      component: defineAsyncComponent(component),
      meta: {
        title: config.title,
        icon: config.icon
      }
    })
  }

  return routes
}

export default generateRoutes